package com.zzyl.nursing.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.huaweicloud.sdk.iotda.v5.model.ServiceCapability;
import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备表Controller
 * 
 * @author alexis
 * @date 2025-09-14
 */
@Api(tags = "设备表管理")
@RestController
@RequestMapping("/nursing/device")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备表列表
     */
    @ApiOperation("查询设备表列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    public TableDataInfo<List<Device>> list(@ApiParam("设备表查询条件") Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 导出设备表列表
     */
    @ApiOperation("导出设备表列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:export')")
    @Log(title = "设备表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@ApiParam(value = "设备表查询条件") HttpServletResponse response, Device device)
    {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "设备表数据");
    }
//
//    /**
//     * 获取设备表详细信息
//     */
//    @ApiOperation("获取设备表详细信息")
//    @PreAuthorize("@ss.hasPermi('nursing:device:query')")
//    @GetMapping(value = "/{id}")
//    public R<Device> getInfo(@ApiParam("设备表ID") @PathVariable("id") Long id)
//    {
//        return R.ok(deviceService.selectDeviceById(id));
//    }

    /**
     * 新增设备表
     */
    @ApiOperation("新增设备表")
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @Log(title = "设备表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam("设备表信息") @RequestBody Device device)
    {
        return toAjax(deviceService.insertDevice(device));
    }


    /**
     * 修改设备
     * @return
     */
    @ApiOperation("修改设备")
    @PutMapping
    public R<String> editDevice(@RequestBody DeviceDto dto){
        deviceService.editDevice(dto);
        return R.ok("修改设备");
    }
    /**
     * 删除设备表
     */
    @ApiOperation("删除设备表")
    @PreAuthorize("@ss.hasPermi('nursing:device:remove')")
    @Log(title = "设备表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{iotId}")
    public AjaxResult remove(@ApiParam("设备表ID数组") @PathVariable String iotId)
    {
        return toAjax(deviceService.deleteDeviceById(iotId));
    }

    /**
     * 从华为云获取设备信息
     * @return
     */
    @ApiOperation("同步产品列表")
    @PostMapping("/syncProductList")
    public R<String> syncProductList() {
        deviceService.syncProductList();
        return R.ok("同步成功");
    }

    /**
     * 获取所有产品信息
     * @return
     */
    @ApiOperation("获取所有产品信息")
    @GetMapping("/allProduct")
    public R<List<ProductVo>> allProduct() {
        return R.ok(deviceService.allProduct());
    }

    /**
     * 注册设备
     * @param dto
     * @return
     */
    @ApiOperation("注册设备")
    @PostMapping("/register")
    public R<String> register(@RequestBody DeviceDto dto){
        deviceService.register(dto);
        return R.ok("注册成功");
    }

    /**
     * 查询设备详情
     * @param iotId 设备ID
     * @return 设备详情
     */
    @ApiOperation("查询设备详情")
    @GetMapping("/{iotId}")
    public R<DeviceDetailVo> getDeviceDetail(@PathVariable String iotId){
        return R.ok(deviceService.getDeviceDetail(iotId));
    }

    /**
     * 查询设备上报的数据
     * @param iotId 设备ID
     * @return 设备上报的数据
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("查询设备上报的数据")
    public R<List<Map<String, Object>>> queryServiceProperties(@PathVariable String iotId){
        return R.ok(deviceService.queryServiceProperties(iotId));
    }

    /**
     * 查询产品详情(报警规则模块使用)
     * @param productKey 产品Key
     * @return 产品详情
     */
    @GetMapping("/queryProduct/{productKey}")
    @ApiOperation("查询产品详情(报警规则模块使用)")
    public R<List<ServiceCapability>> queryProduct(@PathVariable String productKey){
        return R.ok(deviceService.queryProduct(productKey));
    }
}
